package com.newer.controller.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.newer.pojo.sys.Resource;
import com.newer.pojo.sys.Role;
import com.newer.pojo.sys.RoleResource;
import com.newer.service.sys.iter.RoleService;
import com.newer.utils.sys.BootstrapDataTable;
import com.newer.utils.sys.Conditions;
import com.newer.utils.sys.MsgUtil;
import com.newer.utils.sys.Tree;

/**
 * 控制层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("role")
public class RoleController {
	
	//自动载入业务层
	@Autowired
	private RoleService roleService;
	
	@RequestMapping("tableView")
	public @ResponseBody BootstrapDataTable toGetRole(BootstrapDataTable dataTable,Conditions conditions) {
		BootstrapDataTable selectRoleByExample = roleService.selectRoleByExample(dataTable, conditions);
		return selectRoleByExample;
	}
	
	
	@RequestMapping("checkRoleName")
	public @ResponseBody Map<String,Boolean> checkRoleName(String roleName,Integer id){
		Map<String, Boolean> selectRoleByRoelName = roleService.selectRoleByRoelName(roleName,id);
		return selectRoleByRoelName;
	}
	
	@RequestMapping("add")
	public @ResponseBody MsgUtil toAddRole(Role role) {
		try {
			roleService.insertRole(role);
			return MsgUtil.result(true, "添加成功");
		}catch(Exception e) {
			return MsgUtil.result(false, "添加失败");
		}
	}
	
	@RequestMapping("tree")
	public @ResponseBody List<Resource> toGetResource(){
		return roleService.ResourceTree();
	}
	
	/*@RequestMapping("Xtree")
	public @ResponseBody Tree xtree(){
		System.out.println("xtree");
		List<Resource> resourceTree = roleService.ResourceTree();
		Tree tree = null;
		List<Tree> tlt = new ArrayList<Tree>();
		System.out.println(resourceTree.size());
		for (Resource resource : resourceTree) {
			tree  = new Tree();
			tree.setChecked(false);
			tree.setTitle(resource.getResourceName());
			tree.setValue(resource.getId().toString());
			tlt.add(tree);
		}
		tree.setData(tlt);
		System.out.println(tree);
		return tree;
	}*/
	
	@RequestMapping("update")
	public @ResponseBody MsgUtil toUpdateRole(Role role) {
		try {
			roleService.updateRoleByPrimaryKey(role);
			return MsgUtil.result(true, "修改成功");
		} catch (Exception e) {
			return MsgUtil.result(false, "修改失败");
		}
	}
	
	@RequestMapping("delete/{id}")
	public @ResponseBody MsgUtil toDeleteById(@PathVariable("id") Integer id) {
		try {
			roleService.shamRoleById(id);
			return MsgUtil.result(true, "删除成功");
		} catch (Exception e) {
			return MsgUtil.result(false, "删除失败");
		}
	}
	
	@RequestMapping("batchdelete/{ids}")
	public @ResponseBody MsgUtil toDeleteIds(@PathVariable("ids") String Ids) {
		try{
			roleService.deleteRoleByIds(Ids);
			return MsgUtil.result(true, "删除成功");
		}catch(Exception e){
			return MsgUtil.result(false, "删除失败");
		}
	}
	
	@RequestMapping("selectRoleGrant/{id}")
	public String toGetRoleGrant(@PathVariable("id")Integer id,HttpServletRequest request){
			List<RoleResource> selectAuthorByRoleId = roleService.selectAuthorByRoleId(id);
			String resourceids = "";
			for (int i = 0; i < selectAuthorByRoleId.size(); i++) {
				if (i < selectAuthorByRoleId.size() - 1) {
					resourceids += selectAuthorByRoleId.get(i).getResourceId() + ",";
				} else {//最后一个逗号
					resourceids += selectAuthorByRoleId.get(i).getResourceId();
				}
			}
			System.out.println(resourceids);
			// 保存以逗号分隔的资源字符串id
			request.setAttribute("resourceids", resourceids);
			request.setAttribute("roleId", id);
			return "sys/role/role_grant";
	}
	
	
	@RequestMapping("selecRoletById/{id}")
	public String selecRoletById(@PathVariable("id")Integer id,HttpServletRequest request) {
		request.setAttribute("role", roleService.selectRoleById(id));
		return "sys/role/role_edit";
	}
	
	@RequestMapping("grant")
	public @ResponseBody MsgUtil grant(Integer roleId,String rids) {
		try {
			roleService.insertRoleResource(roleId, rids);
			return MsgUtil.result(true, "授权成功！");
		} catch (Exception e) {
			return MsgUtil.result(false, "授权失败！");
		}
	}
}
